package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class BtnListenActivity extends AppCompatActivity {
  ImageView imageView;
  float offset=50.0f;
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.btn_listener_layout);
    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle("基于监听");
    imageView = findViewById(R.id.Img);

    setSupportActionBar(toolbar);
    BindUpButton();
    BindDownButton();
    BindLeftButton();
    BindRightButton();


  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0,1,0,"基于回调");
    menu.add(0,2,1,"异步加载");
    menu.add(1,3,2,"跳到下一个Activity");
    menu.add(1, 4, 3, "跳到上一个Activity");

    return true;
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case 1:
        LoadEventActivity();
        break;
      case 2:
        LoadAnsyActivity();
        break;
      case 3:
        GoToNextActivity();
        break;
      case 4:
        BackToLastActivity();
    }

    return true;
  }

  private void LoadEventActivity() {
    Intent intent = new Intent(this, BtnEventActivity.class);
    startActivity(intent);
  }
  private void LoadAnsyActivity() {
    Intent intent = new Intent(this, AsynTaskActivity.class);
    startActivity(intent);

  }
  public void BackToLastActivity() {
    Intent intent = new Intent(this, ListViewActivity.class);
    startActivity(intent);
  }

  public void GoToNextActivity() {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);

  }
  private void BindUpButton() {
    Button button_up = (Button) findViewById(R.id.button_up);
    button_up.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        float y=imageView.getY();

        if (y > offset) {
          imageView.setY(y - offset);
        } else {
          imageView.setY(0);
        }


      }
    });
  }
  private void BindDownButton() {
    Button button_down = (Button) findViewById(R.id.button_down);
    button_down.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        float y = imageView.getY();
        float height = imageView.getHeight();
        ViewGroup parent = (ViewGroup) imageView.getParent();
        int parentHeight = parent.getHeight();


        if (y+height+offset < parentHeight ) {
          imageView.setY(y + offset);
        } else {
          imageView.setY(parentHeight-height);
        }
      }
    });
  }

  private void BindLeftButton() {
    Button button_left = (Button) findViewById(R.id.button_left);
    button_left.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        float x=imageView.getX();
        if (x > offset) {
          imageView.setX(x - offset);
        }
        else
        {
          imageView.setX(0);

        }
      }
    });
  }
  private void BindRightButton() {
    Button button_right = (Button) findViewById(R.id.button_right);
    button_right.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        float x=imageView.getX();
        float width=imageView.getWidth();

        ViewGroup parent = (ViewGroup) imageView.getParent();
        int parentWidth = parent.getWidth();
        if (x + width + offset < parentWidth) {
          imageView.setX(x + offset);
        } else {
          imageView.setX(parentWidth - width);
        }
      }
    });
  }
}
